package com.bbzn.device.client.service.impl;

import com.bbzn.device.client.dao.video.VideoMapper;
import com.bbzn.device.client.dataobject.Video;
import com.bbzn.device.client.dto.video.VideoDTO;
import com.bbzn.device.client.service.VideoService;
import com.bbzn.device.client.utils.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wulongbo
 * @Date 2021/6/8 14:20
 * @Version 1.0
 */
@Service
public class VideoServiceImpl implements VideoService {

    @Resource
    private VideoMapper videoMapper;

    @Override
    public int deleteByPrimaryKey(Long videoId) {
        return videoMapper.deleteByPrimaryKey(videoId);
    }

    @Override
    public int insert(Video record) {
        return videoMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(Video record) {
        return videoMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(Video record) {
        return videoMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(Video record) {
        return videoMapper.insertSelective(record);
    }

    @Override
    public Video selectByPrimaryKey(Long videoId) {
        return videoMapper.selectByPrimaryKey(videoId);
    }

    @Override
    public int updateByPrimaryKeySelective(Video record) {
        return videoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Video record) {
        return videoMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<Video> list) {
        return videoMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<Video> list) {
        return videoMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<Video> list) {
        return videoMapper.batchInsert(list);
    }

    @Override
    public Integer selectSumAgreeSelective(String userPhone) {
        return videoMapper.selectSumAgreeSelective(userPhone);
    }

    @Override
    public PageInfo<VideoDTO> selectPageSelective(VideoDTO record) {
        record.setStartIndex((record.getPageNum() - 1) * record.getPageSize());
        PageInfo<VideoDTO> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(record.getPageNum());
        pageInfo.setPageSize(record.getPageSize());
        pageInfo.setResultList(videoMapper.selectPageSelective(record));
        int count = videoMapper.selectCountPageSelective(record);
        pageInfo.setTotalRecord(count);
        return pageInfo;
    }


    @Override
    public List<VideoDTO> selectBySelective(Video record) {
        return videoMapper.selectBySelective(record);
    }

    @Override
    public List<VideoDTO> selectIsLikedVideo(String userPhone) {
        return videoMapper.selectIsLikedVideo(userPhone);
    }


    public PageInfo<VideoDTO> selectPageLonginUPhoneSelective(VideoDTO record) {
        record.setStartIndex((record.getPageNum() - 1) * record.getPageSize());
        PageInfo<VideoDTO> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(record.getPageNum());
        pageInfo.setPageSize(record.getPageSize());
        List<VideoDTO> videoList = videoMapper.selectPageLonginUPhoneSelective(record);
        List<VideoDTO> unList = videoList.stream().filter(n -> n.getIsLike() != null && n.getIsLike() == 2).collect(Collectors.toList());   //查询出不需要的数据
        List<VideoDTO> resultList = (List<VideoDTO>) CollectionUtils.subtract(videoList, unList);   //两个List做比较差集
        pageInfo.setResultList(resultList);
        int count = videoMapper.selectCountPageLonginUPhoneSelective(record);
        pageInfo.setTotalRecord(count);
        return pageInfo;
    }

    @Override
    public String selectByAuthorPictureUrl(String userPhone) {
        return videoMapper.selectByAuthorPictureUrl(userPhone);
    }

    @Override
    public List<VideoDTO> getUserVideoInfo(VideoDTO record) {
        return videoMapper.getUserVideoInfo(record);
    }

    @Override
    public PageInfo<VideoDTO> getFollowUserVideoList(VideoDTO record) {
        record.setStartIndex((record.getPageNum() - 1) * record.getPageSize());
        PageInfo<VideoDTO> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(record.getPageNum());
        pageInfo.setPageSize(record.getPageSize());
        List<VideoDTO> videoDTOList = videoMapper.getFollowUserVideoList(record);
        pageInfo.setResultList(videoDTOList);
        List<VideoDTO> countList = videoMapper.findFollowUserVideoList(record);
        int count = countList.size();//videoMapper.getCountFollowUserVideoList(record);   不能用这个方法，因为用了分组再用count数据不对。
        pageInfo.setTotalRecord(count);
        return pageInfo;
    }
}

